<?php

namespace App\Library\Logic;

use App\Library\Requests\Area\AreaInfoRequest;
use App\Library\Requests\Base\IdRequest;
use App\Models\AreaModel;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class AreaLogic extends AreaModel
{
    public static function findAreaInfo(int $parentId, int $id): Model|Builder|null
    {
        return self::query()->where([
            'parent_id' => $parentId,
            'id' => $id
        ])->first(['id', 'name']);
    }

    public function areaInfo(AreaInfoRequest $request): Collection
    {
        $parentId = is_null($request->parentId) ? 0 : $request->parentId;
        return self::getByWhere('parent_id', $parentId);
    }

    public function alls(): array
    {
        return self::query()->with('child')->where('level', 1)->get(['id', 'name', 'code'])->toArray();
    }

    public function level3(): array
    {
        return AreaLogic::getByWhere(['level' => 3, 'status' => 1], null, ['id', 'parentId', 'name', 'code'])->toArray();
    }
}
